package com.shanglida.main;

import android.app.ActionBar.LayoutParams;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.shanglida.R;

/**
 * @author liu
 * @deprecated 主界面切换 原作者忘了
 * @date 2014-02-26 AM
 * @version 1.0
 * @company 广州尚礼达信息科技有限公司
 */
public class MainActivity extends ActivityGroup {
	// 依次为 home页面，msg页面，need页面，set页面，find页面
	private LinearLayout mtb_home, mtb_msg, mtb_need, mtb_set, mtb_find;
	// 界面按钮导航
	public LinearLayout mliner;

	// 左键或者菜单键
	private boolean menu_display = false;
	private PopupWindow menuWindow;
	private LayoutInflater inflater;
	private View layout;
	private LinearLayout mClose;
	private LinearLayout mCloseBtn;

	/*
	 * (non-Javadoc) initView 初始化数据 initData 监听事件
	 * 
	 * @see android.app.ActivityGroup#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_ativity);
		initView();
		initData();
		// 前端设置
		mtb_home.setSelected(true);
		mtb_msg.setSelected(false);
		mtb_need.setSelected(false);
		mtb_find.setSelected(false);
		mtb_set.setSelected(false);
		// 默认加载的activity（底部框架可重用的跳转方式）
		mliner.removeAllViews();
		Intent intentHome = new Intent(MainActivity.this, TabHome.class);
		View view = MainActivity.this.getLocalActivityManager()
				.startActivity("suibian", intentHome).getDecorView();
		mliner.addView(view);
	}

	/**
	 * initData 监听事件
	 */
	private void initData() {
		mtb_home.setOnClickListener(clickListener_home);
		mtb_msg.setOnClickListener(clickListener_msg);
		mtb_need.setOnClickListener(clickListener_need);
		mtb_find.setOnClickListener(clickListener_find);
		mtb_set.setOnClickListener(clickListener_set);
	}

	/**
	 * initView 初始化数据
	 */
	private void initView() {
		mliner = (LinearLayout) findViewById(R.id.sangmainliner1);
		mtb_home = (LinearLayout) findViewById(R.id.first_main_ll);
		mtb_msg = (LinearLayout) findViewById(R.id.second_main_ll);
		mtb_need = (LinearLayout) findViewById(R.id.third_main_ll);
		mtb_find = (LinearLayout) findViewById(R.id.four_main_ll);
		mtb_set = (LinearLayout) findViewById(R.id.fifth_main_ll);
	}

	/**
	 * clickListener_home 主页面 导航
	 */
	private OnClickListener clickListener_home = new OnClickListener() {
		@Override
		public void onClick(View v) {
			mtb_home.setSelected(true);
			mtb_msg.setSelected(false);
			mtb_need.setSelected(false);
			mtb_find.setSelected(false);
			mtb_set.setSelected(false);
			mliner.removeAllViews();
			Intent Homes = new Intent(MainActivity.this, TabHome.class);
			View view = MainActivity.this.getLocalActivityManager()
					.startActivity("suibian", Homes).getDecorView();
			mliner.addView(view);

		}
	};

	/**
	 * msg导航页
	 */
	private OnClickListener clickListener_msg = new OnClickListener() {
		@Override
		public void onClick(View v) {
			mtb_home.setSelected(false);
			mtb_msg.setSelected(true);
			mtb_need.setSelected(false);
			mtb_find.setSelected(false);
			mtb_set.setSelected(false);
			mliner.removeAllViews();
			Intent intentMap = new Intent(MainActivity.this, TabMsg.class);
			View view = MainActivity.this.getLocalActivityManager()
					.startActivity("suibian", intentMap).getDecorView();
			mliner.addView(view);
		}
	};

	/**
	 * need导航页
	 */
	private OnClickListener clickListener_need = new OnClickListener() {

		@Override
		public void onClick(View v) {
			mtb_home.setSelected(false);
			mtb_msg.setSelected(false);
			mtb_need.setSelected(true);
			mtb_find.setSelected(false);
			mtb_set.setSelected(false);
			mliner.removeAllViews();
			Intent intentNed = new Intent(MainActivity.this, TabNeed.class);
			View view = MainActivity.this.getLocalActivityManager()
					.startActivity("suibian", intentNed).getDecorView();
			mliner.addView(view);

		}
	};

	/**
	 * find导航页
	 */
	private OnClickListener clickListener_find = new OnClickListener() {

		@Override
		public void onClick(View v) {
			mtb_home.setSelected(false);
			mtb_msg.setSelected(false);
			mtb_need.setSelected(false);
			mtb_find.setSelected(true);
			mtb_set.setSelected(false);
			mliner.removeAllViews();
			Intent intentQrc = new Intent(MainActivity.this, TabFind.class);
			View view = MainActivity.this.getLocalActivityManager()
					.startActivity("suibian", intentQrc).getDecorView();
			mliner.addView(view);

		}
	};

	/**
	 * 个人设置
	 */
	private OnClickListener clickListener_set = new OnClickListener() {

		@Override
		public void onClick(View v) {
			mtb_home.setSelected(false);
			mtb_msg.setSelected(false);
			mtb_need.setSelected(false);
			mtb_find.setSelected(false);
			mtb_set.setSelected(true);
			mliner.removeAllViews();
			Intent intentSet = new Intent(MainActivity.this, TabSet.class);
			View view = MainActivity.this.getLocalActivityManager()
					.startActivity("suibian", intentSet).getDecorView();
			mliner.addView(view);
		}
	};

	/*
	 * (non-Javadoc)锁定返回键
	 * 
	 * @see android.app.Activity#onKeyUp(int, android.view.KeyEvent)
	 */
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

			if (menu_display) {
				menuWindow.dismiss();
				menu_display = false;
			} else {
				// 重写左键方法
				Intent intentsmall = new Intent();
				intentsmall.setAction("android.intent.action.MAIN");
				intentsmall.addCategory("android.intent.category.HOME");
				startActivity(intentsmall);
			}
		} else if (keyCode == KeyEvent.KEYCODE_MENU) {
			if (!menu_display) {
				inflater = (LayoutInflater) this
						.getSystemService(LAYOUT_INFLATER_SERVICE);

				layout = inflater.inflate(R.layout.exit_blew_menu, null);

				menuWindow = new PopupWindow(layout, LayoutParams.FILL_PARENT,
						LayoutParams.WRAP_CONTENT); // 后两个参数是width和height

				menuWindow.showAtLocation(this.findViewById(R.id.sang_exit),
						Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置

				mClose = (LinearLayout) layout.findViewById(R.id.menu_close);
				mCloseBtn = (LinearLayout) layout
						.findViewById(R.id.menu_close_btn);

				mCloseBtn.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						Intent angodexit = new Intent(MainActivity.this,
								SangExit.class);
						startActivity(angodexit);
						menuWindow.dismiss();
					}
				});
				menu_display = true;
			} else {
				menuWindow.dismiss();
				menu_display = false;
			}
			return false;
		}
		return false;
	}
}
